﻿using System;
using System.Configuration;

namespace Catalyst.Caching.Configuration
{
	public class CacheElementCollection : ConfigurationElementCollection
	{
		public DatabaseElement this[int index]
		{
			get
			{
				return (DatabaseElement)BaseGet(index);
			}
			set
			{
				if (BaseGet(index) != null)
				{
					BaseRemoveAt(index);
				}

				BaseAdd(index, value);
			}
		}

		protected override ConfigurationElement CreateNewElement()
		{
			return new DatabaseElement();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((DatabaseElement)element).Name;
		}
	}

	public class CacheElement : ConfigurationElement
	{
		[ConfigurationProperty("name", IsRequired = true)]
		public string Name
		{
			get { return this["name"].ToString(); }
		}

		[ConfigurationProperty("connectionStringName", IsRequired = true)]
		public string ConnectionStringName
		{
			get { return this["connectionStringName"].ToString(); }
		}

		[ConfigurationProperty("namespace", IsRequired = true)]
		public string Namespace
		{
			get { return this["namespace"].ToString(); }
		}
		
		[ConfigurationProperty("contextNamespace", IsRequired = false)]
		public string ContextNamespace
		{
			get { return this["contextNamespace"].ToString(); }
		}
		
		[ConfigurationProperty("generateNullableProperties", DefaultValue = true, IsRequired = false)]
		public bool GenerateNullableProperties
		{
			get { return Convert.ToBoolean(this["generateNullableProperties"]); }
		}
		
		[ConfigurationProperty("viewStartsWith", DefaultValue = "", IsRequired = false)]
		public string ViewStartsWith
		{
			get { return this["viewStartsWith"].ToString(); }
		}
		
		[ConfigurationProperty("excludeTables", DefaultValue = "", IsRequired = false)]
		public string ExcludeTables
		{
			get { return this["excludeTables"].ToString(); }
		}

		//[ConfigurationProperty("enumTables", DefaultValue = null, IsRequired = false)]
		//public EnumTableElementCollection EnumTables
		//{
		//    get { return (EnumTableElementCollection)this["enumTables"]; }
		//}
	}
}
